/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.wisnu.tutorial.controller;

import com.wisnu.tutorial.model.MahasiswaReader;
import com.wisnu.tutorial.object.MahasiswaObject;
import com.wisnu.tutorial.sql.Connection;
import com.wisnu.tutorial.view.View;
import java.util.List;
import javax.swing.table.DefaultTableModel;

public class MainController {

    private Connection connection;
    private MahasiswaReader mahasiswaReader;
    private View view;

    public MainController(Connection connection) {
        this.connection = connection;
        view = new View();
        tampilData();
        view.setVisible(true);
    }

    public void tampilData() {
        mahasiswaReader = new MahasiswaReader();
        mahasiswaReader.setConnection(connection);
        if (mahasiswaReader.checkData()) {
            List<MahasiswaObject> dataList = mahasiswaReader.getDataMahasiswa();
            String[] header = {"Nim", "Nama", "Prodi"};
            String[][] dataTable = new String[dataList.size()][3];
            int x = 0;
            for (MahasiswaObject object : dataList) {
                dataTable[x][0] = object.getNim();
                dataTable[x][1] = object.getNama();
                dataTable[x][2] = object.getProdi();
                x++;
            }
            view.setTableModel(new DefaultTableModel(dataTable, header));
        } else {
            System.err.println("Error when displaying data");
        }
    }

    //---------------------------//
    public Connection getConnection() {
        return connection;
    }

    public void setConnection(Connection connection) {
        this.connection = connection;
    }

}
